<?php
    $matchId = isset($_POST['matchId']) ? $_POST['matchId'] : die('Invalid matchId');
    list($dump, $matchId) = explode("_", $matchId);
    if (!is_numeric($matchId)) die('Invalid matchId');
    $bfEventName = isset($_POST['bfEventName']) ? $_POST['bfEventName'] : die('Invalid Betfair Event');
    
    require '../Include/BDMySQL.class.php';
    $MySQL = new BDMySQL();
    $MySQL->ligarBD() or die;
    
    global $toXML;
    $toXML = true;
    
    chdir('../');
    $idJogo = $matchId;
    
    header('Content-Type: application/xml;');
    /*require '/paginas/jogos/detalhes.php';
    
    $XML->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="xslt/gamepreview_page.xslt"');
    echo $XML->asXML();
    */
    $XML = new SimpleXMLElement_Plus('<XSMatch
    xmlns="http://rpmestre.net/xcrawler"
    xmlns:XSMatch = "http://www.w3.org/2001/XMLSchema-instance"
    XSMatch:schemaLocation = "http://rpmestre.net/xcrawler XSMatchSchema.xsd">
    </XSMatch>');
    
    $XML->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="xslt/gamepreview_page.xslt"');
   
    
   
    require_once("./Include/Jogo.class.php");
     
     
     $headHTML = '';
     if(!isset($idJogo))
        $idJogo =  $_GET['idJogo'];
     $XML_match = $XML->addchild('match');
     $XML_match->addAttribute('id', 'm_'.$idJogo);
     
     
	 $tJogo = new Jogo();
     $tJogo->GetDetalhesJogo($idJogo);
	 $tJogo->PrintResumoJogo();
     
    $XML_prognosis = $XML_match->addChild('prognosis');
    $incluirAnterior = isset($_POST['incluiranterior']) ? $_POST['incluiranterior']==1 : 0;
    $res =  $tJogo->PrintPrognostico($incluirAnterior, $XML_prognosis);
  
   echo $XML->asXML();
    
    
    class SimpleXMLElement_Plus extends SimpleXMLElement 
    {
        public function addProcessingInstruction( $name, $value )
        {
            // Create a DomElement from this simpleXML object
            $dom_sxe = dom_import_simplexml($this);
           
            // Create a handle to the owner doc of this xml
            $dom_parent = $dom_sxe->ownerDocument;
           
            // Find the topmost element of the domDocument
            $xpath = new DOMXPath($dom_parent);
            $first_element = $xpath->evaluate('/*[1]')->item(0);
           
            // Add the processing instruction before the topmost element           
            $pi = $dom_parent->createProcessingInstruction($name, $value);
            $dom_parent->insertBefore($pi, $first_element);
        }
    }
 ?>